/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package filters;

import core.Filter;
import java.awt.image.BufferedImage;
import java.awt.image.ColorModel;
import java.awt.image.Raster;
import java.awt.image.WritableRaster;
import utils.Constants;

/**
 *
 * @author fuanka
 */
public class RedBandExtractor extends Filter {

    private boolean debug = true;
    private BufferedImage toDecompose;

    public RedBandExtractor() {
        this.hasData = false;
    }

    public Object pull() {
        if (!this.hasData) {
            throw new IllegalStateException("Filter has no data");
        }

        int sourceHeight = toDecompose.getHeight();
        int sourceWidth = toDecompose.getWidth();
        BufferedImage redComponent = new BufferedImage(sourceWidth, sourceHeight, BufferedImage.TYPE_INT_RGB);

        Raster imageRaster=toDecompose.getData();
        WritableRaster targetRaster=redComponent.getRaster();

       
        for (int x = 0; x < sourceWidth; x++) {
            for (int y = 0; y < sourceHeight; y++) {
                targetRaster.setSample(x, y, Constants.redSample, imageRaster.getSample(x, y, Constants.redSample));
            }
        }

        return (Object) redComponent;
    }

    public void push(Object input) {
        try {
            toDecompose = (BufferedImage) input;
            this.hasData = true;
        } catch (ClassCastException cce) {
            System.out.println("ERROR: unsopoted input for filter of " + this.getClass().toString());
            if (debug) {
                cce.printStackTrace();
            }
        }
    }
}
